package com.example.luobaomessage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.os.Handler;
import android.widget.BaseAdapter;

public class LuoBaoYiBiao extends AppCompatActivity {

    private ViewPager2 viewPager2;

    int now;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_luo_bao_yi_biao);
        viewPager2 = findViewById(R.id.viewpager);

        FragmentAdapter adapter = new FragmentAdapter(LuoBaoYiBiao.this);

        viewPager2.setAdapter(adapter);

        Handler handler=new Handler();
        Runnable runnable=new Runnable() {
            @Override
            public void run() {
                now++;
                viewPager2.setCurrentItem(now,true);

                handler.postDelayed(this,3000);
            }
        };handler.postDelayed(runnable,3000);

    }

    private class FragmentAdapter extends FragmentStateAdapter {


        public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch (position%2) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
            }
            return null;
        }


        @Override
        public int getItemCount() {
            return Integer.MAX_VALUE;
        }
    }
}